<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ include file="/WEB-INF/taglib.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
    <title>组织机构管理</title>
    <%@ include file="/WEB-INF/jsp/include.jsp" %>
    <script type="text/javascript">
        var path = "${path}";
        var shStatus = new Array("停用", "启用");
        var params = new Object();
        var url = path + "/system/org/listByPage";

        $(function () {
            //设置AjaxLoading
            setAjaxLoading();
            var grid = $("#tt");
            initGrid(grid);
            ajaxJson('${path}/system/org/getDirectChildrenTreeData', params, singleInitTree);
        });
        //初始化树点击函数
        function zTreeOnClick(event, treeId, treeNode) {
            params.pid = treeNode.id;
            if (!treeNode.pIds) {
                treeNode.pIds = "";
            }
            params.pcode = treeNode.code;
            params.orgCode = treeNode.code;
            treeSingleLoad(params, treeId, treeNode);//调用层级加载树的方法
            btnQuery();

        };

        //       展开树调用方法
        function zTreeOnExpand(event, treeId, treeNode) {
            params.orgCode = treeNode.code;
            treeSingleLoad(params, treeId, treeNode)
        };
        //     树层级加载方法
        function treeSingleLoad(params, treeId, treeNode) {
            var treeObj = $.fn.zTree.getZTreeObj("tree");
            var node = treeNode;
            //没有子节点才去查询
            if (node.children == null || node.children == "undefined") {
                $.ajax({
                    type: "POST",
                    async: true,
                    url: path + "/system/org/getDirectChildrenTreeData",
                    data: params,
                    dataType: "json",
                    success: function (data) {
                        var analyMsg = data;
                        analyMsg.code == "0000"
                        if (analyMsg.code == "0000") {
                            //添加新节点
                            var newNode = treeObj.addNodes(node, data.returnObject);
                        }
                    },
                    error: function (event, XMLHttpRequest, ajaxOptions, thrownError) {
                        $.alert("default", "error", data.responseText);
                    }
                });
            }
        }


        //初始化表格和数据
        function initGrid(grid) {
            var columns = [
                {field: "id", title: "ID", hidden: true, align: "left", sortable: false, resizable: true, width: 80},
                {
                    field: "orgCode",
                    title: "机构代码",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 100
                },
                {
                    field: "orgName",
                    title: "机构名称",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 250
                },
                {
                    field: "orgAliases",
                    title: "机构简称",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 100
                },
                {
                    field: "fullName",
                    title: "机构所在地",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 150
                },
                {
                    field: "businessAddress",
                    title: "经营地址",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 100
                },
                {
                    field: "orgSort",
                    title: "本级序号",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 100
                },
                {
                    field: "validMark",
                    title: "状态",
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 120,
                    formatter: function (value, row, index) {
                        return shStatus[value];
                    }
                },{
                    field: "sjname",
                    title: "上级名称",
                    align: "left",
                    halign: "center",
                    hidden: "true",
                    sortable: false,
                    resizable: true,
                    width: 10
                },{
                    field: "sjcode",
                    title: "上级ID",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    hidden: "true",
                    width: 10
                }
            ];
            initPageGrid(grid, params, url, columns, "#tb");
            var gridOptions = $("#tt").datagrid("options");
            gridOptions.onDblClickRow = function (index, row) {
                btnEditNodeByGrid();
            };
        }

        //查询按钮
        function btnQuery() {
            params.isDisplayAll = $("#isDisplayAll").is(':checked') ? 1 : 0;
            params.orgName = $("#jgname").val();

            var grid = $("#tt");
            var url = path + "/system/org/listByPage";
            getPage(grid, params, url);
        }

        //增加机构
        function btnAddNode() {

            var treeObj = $.fn.zTree.getZTreeObj("tree");
            var nodes = treeObj.getSelectedNodes();
            if (nodes.length == 0) {
                $.alert("default", "info", "添加前请选择一个组织机构树");
                return;
            }
            var sjcode = nodes[0].code;
            var sjname = nodes[0].name;
            var url = encodeURI('${path}/system/org/toAdd?sjcode=' + sjcode + '&sjname=' + sjname );
            var enurl = encodeURI(url);//使用了两次encodeRUI进行编码
            top.layer.open({
                type: 2,
                title: '组织机构 - 添加',
                shadeClose: false,
                shade: 0.3,
                area: ['1000px', '255px'],
                content: enurl,
                end: function callback() {
                    btnQuery();
                }
            });
        }

        //修改机构
        function btnEditNodeByGrid() {
            var rows = $('#tt').datagrid('getSelections');
            if (rows.length == 0) {
                $.alert("pick", "info", "组织机构");
                return;
            }
            var enurl = setUrl();
            top.layer.open({
                type: 2,
                title: '组织机构 - 修改',
                shadeClose: false,
                shade: 0.3,
                area: ['1000px', '255px'],
                content: enurl,
                end: function callback() {
                    btnQuery();
                }
            });

        }
        // 设置url
        function setUrl() {
            var rows = $('#tt').datagrid('getSelections');
            var rowData = rows[0];
            var id = rowData.id
            var sjcode = rowData.sjcode;
            var sjname = rowData.sjname;
            var orgCode = rowData.orgCode;
            var orgName = rowData.orgName;
            var orgAliases = rowData.orgAliases;
            var orgAreaprov = rowData.orgAreaprov;
            var orgAreacity = rowData.orgAreacity;
            var orgAreacontry = rowData.orgAreacontry;
            var businessAddress = rowData.businessAddress;
            var orgSort = rowData.orgSort;
            var pcode = rowData.pcode;
            var url = encodeURI('${path}/system/org/toUpdate?id=' + id + '&sjcode=' + sjcode + '&sjname=' + sjname
                + '&orgCode=' + orgCode + '&orgName=' + orgName + '&orgAliases=' + orgAliases + '&orgAreaprov=' + orgAreaprov + '&orgAreacity=' + orgAreacity
                + '&orgAreacontry=' + orgAreacontry + '&businessAddress=' + businessAddress + '&orgSort=' + orgSort + '&pcode=' + pcode);
            var enurl = encodeURI(url);//使用了两次encodeRUI进行编码
            return enurl;
        }
        //删除机构
        function btnDelNodeByGrid() {
            var rows = $('#tt').datagrid('getSelections');
            if (rows.length == 0) {
                $.alert("pick", "info", "组织机构");
                return;
            }
            var rowData = rows[0];
            var param = new Object();
            param.orgCode = rowData.orgCode;
            param.id = rowData.id;
            $.messager.alert("问题", "请确认是否要删除？","confirm", function () {

                    $.ajax({
                        type: "POST",
                        url: path + '/system/org/toDeleteOrg',
                        data: param,
                        dataType: "json",
                        async: false,
                        success: function callback(data) {
                            var analyMsg = data;
                            if (analyMsg.code != "0000") {
                                $.alert("default", "error", analyMsg.msg);
                            } else {
                                btnQuery();
                                $.alert("default", "info", "删除成功，请刷新组织机构树");
                            }
                        }
                    });

            });

        }
        //启用禁用机构
        function btnYxbzNodeByGrid() {
            var rows = $('#tt').datagrid('getSelections');
            if (rows.length == 0) {
                $.alert("pick", "info", "组织机构");
                return;
            }
            var rowData = rows[0];
            var param = new Object();
            param.id = rowData.id;
            param.validMark = rowData.validMark;
            var mess = "";
            if (param.validMark == "1") {
                mess = "停用后该组织机构及所有下属组织机构关联人员将无法登陆，是否停用？";
            } else {
                mess = "是否启用";
            }
            $.messager.alert("问题", mess,"confirm", function () {

                    $.ajax({
                        type: "POST",
                        url: path + '/system/org/updateByValidMark',
                        data: param,
                        dataType: "json",
                        async: false,
                        success: function callback(data) {
                            var analyMsg = data;
                            if (analyMsg.code != "0000") {
                                $.alert("update", "error", "组织机构");
                            } else {
                                btnQuery();
                            }
                        }
                    });

            });
        }
    </script>
</head>

<body style="margin:0px; padding:0px;">
<%@ include file="/WEB-INF/jsp/loading.jsp" %>
<div class="easyui-layout" fit="true">
    <div region="center" style="padding: 5px;" border="false">
        <div class="easyui-layout" fit="true" border="false">
            <div id="divNorth" region="north" title="查询条件" border="true" noheader="false"
                 style="padding: 6px; height: 92px; background: none repeat scroll 0 0 #EFEFEF;">
                <table>
                    <tr>
                        <td>组织机构名称：</td>
                        <td><input class="query_input" type="text" id="jgname" name="jgname" style="width: 200px"/></td>
                        <td>&nbsp;&nbsp;&nbsp;<input type="checkbox" value="1" name="isDisplayAll"
                                                     id="isDisplayAll" style="vertical-align: text-top;"/><label for="isDisplayAll">显示本下级所有机构</label>
                        </td>
                        <td>
                            <input class="search-btn-blue" style="margin-left:10px" type="button" onclick="btnQuery();"
                                   value="查询">
                        </td>
                    </tr>
                </table>

            </div>
            <div region="center" title="" border="false" noheader="true" style="padding: 5px 0 0 0;">
                <div class="easyui-layout" fit="true" border="false">
                    <div region="west" title="" border="true" noheader="true" split="true" style="width: 350px;">
                        <div class="easyui-layout" fit="true" border="false">
                            <div region="center" title="组织机构树" border="false">
                                <div>
                                    <ul id="tree" class="ztree" style="width: 90%; height: 90%;"></ul>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div region="center" title="组织机构列表信息" border="true">
                        <table id="tt" title="" border="false"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="tb" style="padding: 1px; height: auto; display: none;">
    <div>
        <a id="btn_addNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="false"
           onclick="btnAddNode()">新增</a>
        <a id="btn_editNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="false"
           onclick="btnEditNodeByGrid()">修改</a>
        <a id="btn_delNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="false"
           onclick="btnDelNodeByGrid()">删除</a>
        <a id="btn_qyNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-undo" plain="false"
           onclick="btnYxbzNodeByGrid()">启用/禁用</a>
    </div>
</div>

</body>
</html>
